package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.baidu.customview.R;
import com.baidu.customview.utils.MetricsUtils;

/**
 * Created by imagine on 2015/11/14.
 */
public class ColorFilterView extends View {
    private Paint p;
    private int left, top;
    private Bitmap bitmap;
    public ColorFilterView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        p = new Paint(Paint.ANTI_ALIAS_FLAG);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.peien);
        left = (MetricsUtils.winMetrics( context).widthPixels - bitmap.getWidth())/2;
        top = (MetricsUtils.winMetrics(context).heightPixels - bitmap.getHeight())/2;
        ColorMatrix matrix = new ColorMatrix(new float[]{
                1, 1, 0, 0, 0,   //red 偏移量
                0, 1, 0, 0, 0,   //green偏移量
                0, 0, 1, 0, 0,   //blue偏移量
                0, 0, 0, 1, 1    //transparent偏移量
        });//color=matrix*getColor;getColor为一数组，若这里通过setColor()就是设置的颜色，不然就是默认的黑色
        //note:透明度的值为1才是全显示，0是全透明
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        p.setColorFilter(filter);
    }

    public ColorFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ColorFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ColorFilterView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, left, top, p);
    }
}
